package com.hontech.bread.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.hontech.bread.R
import com.hontech.bread.device.DeviceStatus
import com.hontech.bread.ui.view.LabelState
import com.hontech.bread.ui.view.LabelTextView
import com.hontech.bread.utils.isBitSet
import com.hontech.bread.utils.registerEvent
import com.hontech.bread.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SwSensorParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val sws: Array<LabelState>
    private val lbAdc: LabelTextView

    private val swPt100: LabelState
    private val ltTemp: LabelTextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_sw_sensor, this)
        lbAdc = view.findViewById(R.id.id_sw_adc)
        swPt100 = view.findViewById(R.id.id_sw_pt100)
        ltTemp = view.findViewById(R.id.id_sw_lt_temp)
        sws = arrayOf(
            view.findViewById(R.id.id_sw_sensor_sw0),
            view.findViewById(R.id.id_sw_sensor_sw1),
            view.findViewById(R.id.id_sw_sensor_sw2),
            view.findViewById(R.id.id_sw_sensor_sw3),
            view.findViewById(R.id.id_sw_sensor_sw4),
            view.findViewById(R.id.id_sw_sensor_sw5),
            view.findViewById(R.id.id_sw_sensor_sw6),
            view.findViewById(R.id.id_sw_sensor_sw7),
            view.findViewById(R.id.id_sw_sensor_sw8),
            view.findViewById(R.id.id_sw_sensor_sw9),
            view.findViewById(R.id.id_sw_sensor_sw10),
            view.findViewById(R.id.id_sw_sensor_sw11),
            view.findViewById(R.id.id_sw_sensor_sw12),
            view.findViewById(R.id.id_sw_sensor_sw13),
            view.findViewById(R.id.id_sw_sensor_sw14),
            view.findViewById(R.id.id_sw_sensor_sw15),
            view.findViewById(R.id.id_sw_sensor_sw16),
            view.findViewById(R.id.id_sw_sensor_sw17),
            view.findViewById(R.id.id_sw_sensor_sw18),
            view.findViewById(R.id.id_sw_sensor_sw19),
            view.findViewById(R.id.id_sw_sensor_sw20),
            view.findViewById(R.id.id_sw_sensor_sw21),
            view.findViewById(R.id.id_sw_sensor_sw22),
            view.findViewById(R.id.id_sw_sensor_sw23),
            view.findViewById(R.id.id_sw_sensor_sw24),
            view.findViewById(R.id.id_sw_sensor_sw25),
            view.findViewById(R.id.id_sw_sensor_sw26),
            view.findViewById(R.id.id_sw_sensor_sw27),
            view.findViewById(R.id.id_sw_sensor_sw28),
            view.findViewById(R.id.id_sw_sensor_sw29)
        )
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            registerEvent(this)
        } else {
            unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun update(status: DeviceStatus) {
        for (i in sws.indices) {
            val sw = sws[i]
            sw.set(status.swSensor.isBitSet(i))
        }
        lbAdc.set(status.adc)
        ltTemp.set(status.pt100)
        swPt100.set(status.isPt100Ok())
    }

}




